<template>
  <!-- 模具操作记录表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-if="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="成品物料编号" prop="materialCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialCode"
              placeholder="请输入成品物料编号"
              clearable
            />

            <span v-else>{{ formData.materialCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成品物料名称" prop="materialName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialName"
              placeholder="请输入成品物料名称"
              clearable
            />

            <span v-else>{{ formData.materialName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="mouldCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.mouldCode"
              placeholder="请输入模具编号"
              clearable
            />

            <span v-else>{{ formData.mouldCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具名称" prop="mouldName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.mouldName"
              placeholder="请输入模具名称"
              clearable
            />

            <span v-else>{{ formData.mouldName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具类型" prop="mouldType">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.mouldType"
              placeholder="请输入模具类型"
              clearable
            />

            <span v-else>{{ formData.mouldType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具位置名称" prop="locationName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.locationName"
              placeholder="请输入模具位置名称"
              clearable
            />

            <span v-else>{{ formData.locationName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="操作类型" prop="workType">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.workType"
              placeholder="请选择操作类型"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('MoldWorkKind')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName("MoldWorkKind", formData.workType)
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="操作人" prop="workUser">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workUser"
              placeholder="请输入操作人"
              clearable
            />

            <span v-else>{{ formData.workUser }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="操作时间" prop="workTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workTime"
              placeholder="请输入操作时间"
              clearable
            />

            <span v-else>{{ formData.workTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="创建人" prop="createdUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              placeholder="请输入创建人"
              clearable
            />

            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新时间" prop="lastUpdateTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              placeholder="请输入更新时间"
              clearable
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="更新人名" prop="lastUpdatedUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserName"
              placeholder="请输入更新人名"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-descriptions
      v-if="props.isReadonly"
      border
      :column="3"
      v-show="props.isReadonly"
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">成品物料编号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.materialCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">成品物料名</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.materialName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具编号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.mouldCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具名称</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.mouldName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具类型</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.mouldType }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具位置名称</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.locationName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">操作类型</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{
            dictionaryStore.filterDictName("MoldWorkKind", formData.workType)
          }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">操作人</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.workUser }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">操作时间</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.workTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.createdUserName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.createTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.lastUpdatedUserName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新时间</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.lastUpdateTime }}
        </div>
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "WMSEAMMOULDWORKRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      // formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  materialCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  mouldCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  mouldName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  mouldType: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  locationName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  workType: [],
  workUser: [
    { min: 1, max: 64, message: "长度在 1 到 64 个字符", trigger: "blur" },
  ],
  workTime: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  materialCode: "",
  materialName: "",
  mouldCode: "",
  mouldName: "",
  mouldType: "",
  locationId: "",
  locationName: "",
  workType: "",
  workUser: "",
  workTime: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        materialCode,
        materialName,
        mouldCode,
        mouldName,
        mouldType,
        locationId,
        locationName,
        workType,
        workUser,
        workTime,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        materialCode,
        materialName,
        mouldCode,
        mouldName,
        mouldType,
        locationId,
        locationName,
        workType,
        workUser,
        workTime,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  // formRef.value.resetFields();
  // formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.mouldCode = "";
  formData.mouldName = "";
  formData.mouldType = "";
  formData.locationId = "";
  formData.locationName = "";
  formData.workType = "";
  formData.workUser = "";
  formData.workTime = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 模具操作记录表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/eammouldworkrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style lang="scss" scoped>
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 110px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 100px;
}
</style>
